package com.tw.trips.ui;

import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.stereotype.Service;

import com.vaadin.terminal.ThemeResource;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.Component;
import com.vaadin.ui.Embedded;
import com.vaadin.ui.GridLayout;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.Panel;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;
import com.vaadin.ui.themes.BaseTheme;

@Service
@Configurable(preConstruction = true)
public class UpdatePane extends Panel implements ClickListener{
	Window subwindow;
	
	public UpdatePane() {
		
		//Panel superPanel = new Panel();
		//this.setWidth("430px");
		final GridLayout grid = new GridLayout(1, 3);
		this.addComponent(grid);
        grid.setSpacing(true);

        Panel commentsPanel = new Panel();
		//main update - not scrollable
        grid.addComponent(includeMainUpdateHorzLayout(),0,0);
		//comments - scrollable area
        grid.addComponent(commentsPanel,0,1);
        //add button
        grid.addComponent(includeAddCommentPane(),0,2);
        
		
		//dimensions
		commentsPanel.setHeight("500px");
		commentsPanel.setWidth("300px");
		
		//setting a vertical layout inside main panel
		VerticalLayout mainVerticalLayout = new VerticalLayout();
		mainVerticalLayout.setSpacing(true);
		commentsPanel.setContent(mainVerticalLayout);
		
		
		
		//as many times as comments are found
		for(int i=0;i<10;i++) {
			HorizontalLayout comment = includeCommentHorzLayout("This is a sample Comment");
			mainVerticalLayout.addComponent(comment);
			mainVerticalLayout.setComponentAlignment(comment, Alignment.MIDDLE_CENTER);
		}
		
		HorizontalLayout comment = includeCommentHorzLayout("This is a sample Comment This is a sample Comment This is a sample Comment This is a sample Comment This is a sample Comment This is a sample Comment");
		mainVerticalLayout.addComponent(comment);
		mainVerticalLayout.setComponentAlignment(comment, Alignment.MIDDLE_CENTER);
		//return superPanel;
	}

	private Component includeAddCommentPane() {
		// Button w/ text and tooltip
        Button addCommentButton = new Button("Add Comment");
        addCommentButton.setStyleName(BaseTheme.BUTTON_LINK);
        addCommentButton.setDescription("To add comment");
        addCommentButton.addListener(this); // react to clicks
        return addCommentButton;
	}

	private HorizontalLayout includeCommentHorzLayout(String string) {
		HorizontalLayout mainUpdateHorzLayout = new HorizontalLayout();
		mainUpdateHorzLayout.setSpacing(true);
		//mainUpdateHorzLayout.setWidth("500px");
		Embedded commentedByPic = new Embedded("",
                new ThemeResource("../runo/icons/16/user.png"));
		Label commentedBy = new Label("Prasad");
		Label comment = new Label(string);
		
		Embedded itHelps = new Embedded("",
                new ThemeResource("../runo/icons/16/arrow-up.png"));
		Label itHelpsCount = new Label("2");
		
		mainUpdateHorzLayout.addComponent(commentedByPic);
		mainUpdateHorzLayout.setComponentAlignment(commentedByPic, Alignment.MIDDLE_CENTER);
		mainUpdateHorzLayout.addComponent(commentedBy);
		mainUpdateHorzLayout.setComponentAlignment(commentedBy, Alignment.MIDDLE_CENTER);
		mainUpdateHorzLayout.addComponent(comment);
		mainUpdateHorzLayout.setComponentAlignment(comment, Alignment.MIDDLE_CENTER);
		//TODO : include it helps count
		mainUpdateHorzLayout.addComponent(itHelps);
		mainUpdateHorzLayout.setComponentAlignment(itHelps, Alignment.MIDDLE_RIGHT);
		
		mainUpdateHorzLayout.addComponent(itHelpsCount);
		mainUpdateHorzLayout.setComponentAlignment(itHelpsCount, Alignment.BOTTOM_CENTER);
		
		return mainUpdateHorzLayout;
	}

	private HorizontalLayout includeMainUpdateHorzLayout() {
		HorizontalLayout mainUpdateHorzLayout = new HorizontalLayout();
		mainUpdateHorzLayout.setSpacing(true);
		Embedded updatedByPic = new Embedded("",new ThemeResource("../runo/icons/32/user.png"));
		Label updatedByLabel = new Label("Sripaul");
		Label updateTxt = new Label("This is a sample update");
		Embedded itHelps = new Embedded("",
                new ThemeResource("../runo/icons/32/arrow-up.png"));
		
		Label itHelpsCount = new Label("2");
		
		mainUpdateHorzLayout.addComponent(updatedByPic);
		mainUpdateHorzLayout.setComponentAlignment(updatedByPic, Alignment.MIDDLE_CENTER);
		mainUpdateHorzLayout.addComponent(updatedByLabel);
		mainUpdateHorzLayout.setComponentAlignment(updatedByLabel, Alignment.MIDDLE_CENTER);
		mainUpdateHorzLayout.addComponent(updateTxt);
		mainUpdateHorzLayout.setComponentAlignment(updateTxt, Alignment.MIDDLE_CENTER);
		//TODO : include it helps count
		mainUpdateHorzLayout.addComponent(itHelps);
		mainUpdateHorzLayout.setComponentAlignment(itHelps, Alignment.MIDDLE_RIGHT);
		
		mainUpdateHorzLayout.addComponent(itHelpsCount);
		mainUpdateHorzLayout.setComponentAlignment(itHelpsCount, Alignment.BOTTOM_CENTER);
		
		
		return mainUpdateHorzLayout;
	}

	public void buttonClick(ClickEvent event) {
		
		
		// Create the window...
        subwindow = new Window("Your say?");
        // ...and make it modal
        subwindow.setModal(true);
        subwindow.setHeight("50px");
        subwindow.setWidth("400px");

        // Configure the windws layout; by default a VerticalLayout
        VerticalLayout layout = (VerticalLayout) subwindow.getContent();
        layout.setMargin(true);
        layout.setSpacing(true);

        // Add some content; a label and a close-button
        //Label message = new Label("This is a modal subwindow.");
        //subwindow.addComponent(message);
        
        com.vaadin.ui.TextArea editor = new com.vaadin.ui.TextArea(null, "");
        editor.setRows(2);
        editor.setColumns(28);
        subwindow.addComponent(editor);

        Button addCommentButton = new Button("Add Comment", new Button.ClickListener() {
            // inline click-listener
            public void buttonClick(ClickEvent event) {
                // close the window by removing it from the parent window
                (subwindow.getParent()).removeWindow(subwindow);
            }
        });
        // The components added to the window are actually added to the window's
        // layout; you can use either. Alignments are set using the layout
        layout.addComponent(addCommentButton);
        layout.setComponentAlignment(addCommentButton, Alignment.TOP_RIGHT);
		getWindow().addWindow(subwindow);
		
	}
}
